<script type="text/javascript">
	$(document).ready(function(){
		

		var form_action = "<?php echo $action; ?>";
		
		if(form_action == "add"){
			//$("#name_label,#country_label").addClass("required");
		}else if(form_action == "edit"){
			//$("#name_label,#country_label").addClass("required");
			
			$("#team_id").val("<?php echo isset($team)?$team[0]->teamid:""; ?>");
			$("#team_logo").val("<?php echo isset($team)?$team[0]->team_logo:""; ?>");
			$("#team_name").val("<?php echo isset($team)?$team[0]->team_name:""; ?>");
			$("#team_country").val("<?php echo isset($team)?$team[0]->team_country:""; ?>");
			$("#team_type").val("<?php echo isset($team)?$team[0]->team_type:""; ?>");
			$enabled = "<?php echo isset($team)?$team[0]->enabled:""; ?>";
			if($enabled == "1"){
				$("#team_enabled").attr("checked","checked");				
			}else{
				$("#team_enabled").removeAttr("checked");
			}
		}else{
			alert("Operation Invalid.");
		}

		$("#save").on('click', function (event) {


			
			var url = "<?php echo base_url(); ?>index.php/team/";

			if (form_action == "add"){
				url += "doCreate";
			}else if(form_action == "edit"){
				url += "doEdit";
			}

			 var form_data = $('form').serialize();
			
			var request = $.ajax({
				url: url,
				type: "POST",			
			    async: false,
		        dataType: "html",
		        data: form_data
			});

			request.done(function(msg) {				
				if (form_action == "add"){
					$("#team_name").val("");
					$("#team_enabled").attr("checked","checked");			
				}			
				alert("Data Saved");		
			});

			request.fail(function(jqXHR, textStatus) {
				alert( "Request failed: " + textStatus );
			});
		});
		

		$("#back").on('click', function (event) {
			window.location = "<?php echo base_url(); ?>index.php/team";
		});


		//For Excel file upload
        var test = $("#userfile").makeAsyncUploader({
            upload_url: "<?php echo base_url(); ?>index.php/common/doUpload?page=teams", // Important! This isn't a directory, it's a HANDLER such as an ASP.NET MVC action method, or a PHP file, or a Classic ASP file, or an ASP.NET .ASHX handler. The handler should save the file to disk (or database).
            flash_url: '<?php echo base_url(); ?>asset/js/jquery.asyncupload/swfupload.swf',
            button_image_url: '<?php echo base_url(); ?>asset/js/jquery.asyncupload/blankButton.png',
            file_types: '*.jpg;*.png',
            file_types_description: 'All Picture Files',
            file_size_limit: '10 MB',
            disableDuringUpload: 'INPUT[type="submit"]',
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT
        });
		
		
	});
</script>
<img id="top" src="top.png" alt="">
<div id="form_container">


	<form id="form_889069" class="appnitro" method="post" action="" >
		<div class="form_description">
			<h2><?php echo $title; ?></h2>
		</div>
		<ul>

			<li id="li_1"><label class="description" id="name_label">Team Name</label>
				<div>
					<input id="team_name" name="team_name"
						class="element text medium" type="text" maxlength="200" value="" />
				</div></li>
			<li id="li_2"><label class="description" id="country_label">Team Country</label>
				<div>
									<?php echo country_dropdown('team_country', 'team_country', 'small select', 'TH', array('TH','MY','ID','SG'), ''); ?> 
				</div></li>
			<li id="li_2"><label class="description" id="type_label">Team Type</label>
				<div>
					<select id="team_type" name="team_type">
						<option value="Major" selected>Major</option>
						<option value="Minor">Minor</option>
						<option value="Other">Other</option>
					</select>						
				</div>
			</li>
				<li id="li_1"><label class="description" id="team_logo_label">Team Logo</label>
				<div>
					<input id="userfile" name="userfile" class="element file" type="file" value="" />
					<input type="hidden" id="team_logo" name="team_logo" value="" />
				</div></li>
			<li id="li_3"><label class="description" for="element_3"> </label> <span>
					<input id="team_enabled" name="team_enabled"
					class="element checkbox " type="checkbox" value="1"
					checked="checked" /> <label class="choice" for="element_3_1">Enabled</label>

			</span></li>

			<li class="buttons"><input type="hidden" id="team_id"
				name="team_id" value="" /> <input type="submit" value="Save"
				id="save" class="button_text" /> <input type="button" value="Back"
				id="back" class="button_text" /></li>
		</ul>
	</form>
</div>
